<?php
/**********************
**room.inc
**Date Created: 4/13/2009
**Copyright 2009 Tomorrow Tech Industries
**Contact: RPITomorrowTech@gmail.com
**********************/

require_once("shared/errors.inc");
require_once("dba.inc");

class Room
{	
	//For debugging
	private static $lastError;

	/*******
	Member variables
	*******/
	private $id;

	/********
	Initialization
	********/
	
	//Constructor
	public function __construct($roomID)
	{
		$this->id = $roomID;
	}
	
	//Create an empty room
	static function createNewRoom(&$roomID, $fields)
	{
		//FIXME: how do we get back the room Id?
		//There should only be a single room
		$name = '0';
		if( isset($fields['name']) )
		{
			$name = $fields['name'];
		}
		
		//Escape the values
		$name = DBaccess::GetSQLValueString($name, 'text');

		//id is auto-incremented to form its value
		$q = "INSERT INTO Room 
				(name) 
				VALUES ($name)";
		$db = new DBaccess(); 
		$rc = $db->executeQuery($q, $result);
		if( TT_FAILED($rc) )
		{
			Room::setLastError("Couldn't execute query: " . $q);
			return $rc;
		}
		
		//Seems auto increment will not repeat deleted values (ie even if holes are present)
		$q = "SELECT MAX(roomID) FROM Room WHERE name=$name";
		$rc = $db->executeQuery($q, $result);
		if( TT_FAILED($rc) )
		{
			Room::setLastError("Couldn't execute query: " . $q);
			return $rc;
		}
		$row = mysql_fetch_array($result);
		$roomID = $row[0];
		
		return TT_OK;
	}
	
	/*********
	Utility
	*********/
	
	//Returns an array of room Ids
	public static function getRooms(&$rooms, $all = null)
	{
		$q = "SELECT roomID from Room ORDER BY roomID";
		if($all)
		{
			$q = "SELECT * from Room ORDER BY name";
		}
		$db = new DBaccess(); 
		$rc = $db->executeQuery($q, $result);
		if( TT_FAILED($rc) )
		{
			Room::setLastError("Couldn't execute query: " . $q);
			return $rc;
		}
		$rooms = array();

		if($all)
		{
			//Add in the room information
			while( $row = mysql_fetch_array($result) )
			{
				array_push($rooms, array( 'roomID' => $row[0], 'name' => $row[1]) );
			}
		}
		else
		{
			//Add in all of the room IDs
			while( $row = mysql_fetch_array($result) )
			{
				array_push($rooms, $row[0]);
			}
		}

		return TT_OK;
	}
		
	//All get requests are essentially the same
	//Get a single column value matching the current room id
	private function getColumn($column, &$value)
	{
		//There should only be a single room
		$q = "SELECT $column FROM Room WHERE roomID='{$this->id}' LIMIT 1";
		$db = new DBaccess(); 
		$rc = $db->executeQuery($q, $result);
		if( TT_FAILED($rc) )
		{
			Room::setLastError("Couldn't execute query: " . $q);
			return $rc;
		}
		$row = mysql_fetch_array($result);
		$value = $row[0];
		return TT_OK;
	}
		
	public static function getLastError()
	{
		return Room::$lastError;
	}
	
	public static function clearLastError()
	{
		Room::$lastError = '';
	}
	
	protected static function setLastError($error)
	{
		Room::$lastError = $error;
	}
	
	//All set requests are essentially the same
	//Update a single column matching the current room id to a certain value
	//Value must already be escaped prior to entering this function
	private function setColumn($column, $value)
	{
		//There should only be a single room
		//Example: the following are equivilent:
		$q = "UPDATE Room SET $column=$value WHERE roomID={$this->id} LIMIT 1";
		$db = new DBaccess(); 
		$rc = $db->executeQuery($q, $result);
		if( TT_FAILED($rc) )
		{
			Room::setLastError($db->getLastError());
			return $rc;
		}

		return TT_OK;
	}

	/*****
	Accessor/mutator
	*****/
	
	public function getRoomID(&$roomID)
	{
		$roomID = $this->id;
		return TT_OK;
	}
	
	//Room id is relativly immutable, in this object
	//It should only be changed if the database itself is changed
	public function setRoomID($roomID)
	{
		$roomID = DBaccess::GetSQLValueString($roomID, 'int');
		$rc = $this->setColumn("roomID", $roomID);
		//Only update this id if it really was updated
		if( TT_SUCCEEDED($rc) )
		{
			$this->id = $roomID;
		}
		return $rc;
	}

	public function getName(&$name)
	{
		return $this->getColumn("name", $name);
	}

	public function setName($name)
	{
		return $this->setColumn("name", DBaccess::GetSQLValueString($name, 'text'));
	}
	
	public function getString()
	{
		$ret = "id: " . $this->id;
		return $ret;
	}

	//Test it
	public static function testRoom()
	{
		//Room::createNewRoom($roomID, array('type' => '1', 'name' => 'VCR'));
		if( TT_FAILED(Room::getRooms($rooms)) )
		{
			echo "Failed to get rooms!<BR>";
		}
		else
		{
			if( count($rooms) == 0 )
			{
				echo "No rooms!<BR>\n";
			}
			else
			{
				echo "Rooms:<BR>\n";
				foreach( $rooms as $key => $value )
				{
					echo "Room id: $value<BR>";
					$room = new Room($value);
					
					Room::testGetter($room, "getName");
				}
			}
		}
	
		return true;
	}

	//Print member function tests
	public static function testGetter($room, $function)
	{
		$rc = $room->$function($ret);
		//Begine the print
		echo "&nbsp;&nbsp;&nbsp;$function: ";
		if( TT_SUCCEEDED($rc) )
		{
			echo "$ret";
		}
		else
		{
			echo "ERROR: " . errorString($rc);
		}
		//End the line
		echo "<BR>\n";
		return $rc;
	}
}


?>